import pytest
from selenium import webdriver
import logging
import os

#日志配置
logging.basicConfig(level=logging.INFO)

#浏览器对象
driver=None

#用于每次测试前初始化浏览器对象,测试后退出浏览器
@pytest.fixture(scope='session',autouse=True)
def browser(default_time=30):
    global driver
    #---------------------------------SET UP--------------------------------
    if driver is None:
        driver=webdriver.Firefox()

    #用于设置元素等待
    driver.implicitly_wait(time_to_wait=default_time)
    yield driver
    #--------------------------------TEAR DOWN--------------------------------
    logging.info("End the test".center(50,'-'))
    #保存测试结果
    screenshot_path = os.path.join(os.getcwd(), 'result.png')
    driver.save_screenshot(screenshot_path)
    logging.info(f"截图已保存到 {screenshot_path}")
    #退出浏览器
    driver.quit()


